package com.yjf.ljr.activity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.yjf.ljr.R;
import com.yjf.ljr.dataclass.InvestDetailProjectClass;
import com.yjf.ljr.dataclass.InvestDetailProjectClass.ProjectItemInfo;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.HtmlFormat;

public class ProjectRelationInfoActivity extends IjActivity {

	@ID("webView")
	private WebView webView;
	
	public static  final int PROJECT_INFO = 1;//项目信息
	public static  final int BASE_RATE = 2;//基础评级  项目详情
	public static  final int BLUEPRINT = 3;//计划蓝图
	public static  final int TEAM_INTO = 4;//团队介绍
	
	private int type = PROJECT_INFO;
	
	private String demandId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_project_relation_info);	
		type = getIntent().getIntExtra("type", PROJECT_INFO);
		demandId = getIntent().getStringExtra("demandId");
		initControl();
		new ProjectDetailTask().execute();
	}

	private void initControl() {
		setLeftBtnClick();
		
		WebSettings settings = webView.getSettings();
		webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		settings.setDomStorageEnabled(true);
		settings.setAppCacheMaxSize(1024 * 1024 * 9);// 设置缓冲大小
		String appCacheDir = mContext.getDir("cache", Context.MODE_PRIVATE).getPath();
		settings.setAppCachePath(appCacheDir);
		settings.setAllowFileAccess(true);
		settings.setAppCacheEnabled(true);
		settings.setCacheMode(WebSettings.LOAD_DEFAULT);
//		settings.setUseWideViewPort(true);
		//自适应屏幕
//		settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//		settings.setLoadWithOverviewMode(true);
		switch(type) {
		case PROJECT_INFO:
			setTitleStr("项目简介");
			break;
		case BASE_RATE:
			setTitleStr("项目详情");
			break;
		case BLUEPRINT:
			setTitleStr("计划蓝图");
			break;
		case TEAM_INTO:
			setTitleStr("团队介绍");
			break;
		}
	}
	
	class ProjectDetailTask extends AsyncTask<Void, Void, String> {

		private InvestDetailProjectClass dc = new InvestDetailProjectClass();
				
		@Override
		protected void onPreExecute() {		
			super.onPreExecute();
			showProgressDialog();
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject reqObj = new RequestObject();
			reqObj.method = "appProjectDetail";
			reqObj.map.put("demandId", demandId);
			return requestDataAndGetErrorMsg(reqObj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			dismissProgressDialog();
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				if (dc.projectList != null && dc.projectList.size() > 0) {
					ProjectItemInfo info = (ProjectItemInfo) dc.projectList.get(0);
					if(info != null) {
						switch(type) {
						case PROJECT_INFO:
							if(!TextUtils.isEmpty(info.loanNote)) {
								webView.loadDataWithBaseURL(null, info.loanNote, "text/html", "utf-8", null);
							}
							break;
						case BASE_RATE:
							if(!TextUtils.isEmpty(info.companyInfo)) {
//								webView.loadDataWithBaseURL(null, info.companyInfo, "text/html", "utf-8", null);
								webView.loadDataWithBaseURL(null, HtmlFormat.getNewContent(info.companyInfo), "text/html", "utf-8", null);
							}						
							break;
						case BLUEPRINT:
							if(!TextUtils.isEmpty(info.loanerInfo)) {
								webView.loadDataWithBaseURL(null, info.loanerInfo, "text/html", "utf-8", null);
							}
							break;
						case TEAM_INTO:
							if(!TextUtils.isEmpty(info.projectInfo)) {
								webView.loadDataWithBaseURL(null, info.projectInfo, "text/html", "utf-8", null);
							}
							break;
						}
					}
				}
			}
			
		}
	}
	
	
}
